﻿using System;
using System.Linq;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;//引入正则表达式命名空间
using System.Text;

namespace msBsc.WebUi
{
    public partial class mfVehicleList : msBsc.Cls.mbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                //ShowData();
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        private void ShowData()
        {
            oGoodsSearch = new AppLogic.DataAlc.GoodsSearch(HttpUtility.UrlDecode(this.Request.QueryString.ToString()), "0");
            this.ExecJScript("$('#txtQuery').val('" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(oGoodsSearch.GetQueryStr("", "")) + "');");
            //LoadTopForm();
            //LoadGoodsList("", "", "");
        }

        public string GetTopFormJavaScript()
        {
            System.Text.StringBuilder sbJson = new StringBuilder();
            string strf = oGoodsSearch.dicQuery.ContainsKey("f") ? oGoodsSearch.dicQuery["f"] : "";
            sbJson.Append("{");
            sbJson.Append("\"f\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(strf) + "\"");
            sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(oGoodsSearch.GetQueryStr("", "")) + "\"");
            sbJson.Append(",\"nav_rows\":" + oGoodsSearch.GetVehicleLinkJosn());
            sbJson.Append(",\"form_rows\":" + oGoodsSearch.GetVehicleFilter());
            sbJson.Append("}");
            //return " <script type=\"text/javascript\"> var topFormData=" + sbJson.ToString() + "</script>";
            this.ExecJScript("ShowTopSeachInfo(" + sbJson.ToString() + ")");
            return "";
        }

        public string GetGoodsListJavaScript()
        {
            return " <script type=\"text/javascript\">var goodsListData=" + oGoodsSearch.GetGoodsList("", "", "") + "</script>";
        }

        public string GetVehicleListJavaScript()
        {
            objVehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo(HttpUtility.UrlDecode(Request.QueryString.ToString()));

            #region 获取前台参数
            string strf = objVehicleInfo.dicQuery.ContainsKey("f") ? objVehicleInfo.dicQuery["f"] : "";
            string page = objVehicleInfo.dicQuery.ContainsKey("page") ? objVehicleInfo.dicQuery["page"] : "";

            string country = objVehicleInfo.dicQuery.ContainsKey("country") ? objVehicleInfo.dicQuery["country"] : "";
            string city = objVehicleInfo.dicQuery.ContainsKey("city") ? objVehicleInfo.dicQuery["city"] : "";
            string des = objVehicleInfo.dicQuery.ContainsKey("des") ? objVehicleInfo.dicQuery["des"] : "";

            int vtype = 0;
            if (objVehicleInfo.dicQuery.ContainsKey("vtype") && !string.IsNullOrEmpty(objVehicleInfo.dicQuery["vtype"]))
            {
                int.TryParse(objVehicleInfo.dicQuery["vtype"], out vtype);
            }
            int brand = 0;
            if (objVehicleInfo.dicQuery.ContainsKey("brand") && !string.IsNullOrEmpty(objVehicleInfo.dicQuery["brand"]))
            {
                int.TryParse(objVehicleInfo.dicQuery["brand"], out brand);
            }
            int stype = 0;

            if (objVehicleInfo.dicQuery.ContainsKey("stype") && !string.IsNullOrEmpty(objVehicleInfo.dicQuery["stype"]))
            {
                int.TryParse(objVehicleInfo.dicQuery["stype"], out stype);
            }
            string vprice = objVehicleInfo.dicQuery.ContainsKey("vprice") ? objVehicleInfo.dicQuery["vprice"] : "0|0";
            string seats = objVehicleInfo.dicQuery.ContainsKey("seats") ? objVehicleInfo.dicQuery["seats"] : "0|0";

            decimal maxPirce = 0, minPirce = 0;
            decimal.TryParse(vprice.Split('|')[1], out maxPirce);
            decimal.TryParse(vprice.Split('|')[0], out minPirce);


            int maxSeats = 0, minSeats = 0;
            int.TryParse(seats.Split('|')[1], out maxSeats);
            int.TryParse(seats.Split('|')[0], out minSeats);
            #endregion


            string queryStr = "country=" + country + "&city=" + city + "&des=" + des + "&vtype=" + vtype + "&brand=" + brand + "&vprice=" + vprice + "&seats=" + seats + "&stype=" + stype + "&f=" + strf;
            //设置翻页时的参数
            //string queryStr = "country=" + HttpUtility.UrlEncode(country) + "&city=" + HttpUtility.UrlEncode(city) + "&des=" + HttpUtility.UrlEncode(des) + "&vtype=" + vtype + "&brand=" + brand + "&vprice=" + HttpUtility.UrlEncode(vprice) + "&seats=" + HttpUtility.UrlEncode(seats) + "&stype=" + stype + "&f=" + HttpUtility.UrlEncode(strf);

            string strf1 = string.IsNullOrEmpty(strf) ? string.Empty : "%" + strf + "%";

            country = string.IsNullOrEmpty(country) ? "DF6723BE-A25C-4F77-BB49-A1662D3E6F2A" : country;
            city = string.IsNullOrEmpty(city) ? "AEF39D8E-D298-4AF1-AEC5-2B00D237F5F5" : city;

            return "<script type='text/javascript'>doBoundGoodsList(" + objVehicleInfo.GetVehicleList(page, "", "", des, city, country, vtype, brand, minPirce, maxPirce, minSeats, maxSeats, stype, strf1) + ",'" + queryStr + "','" + strf + "');</script>";


        }

        private msBsc.AppLogic.DataAlc.GoodsSearch oGoodsSearch;

        private msBsc.AppLogic.DataAlc.CBizVehicleInfo objVehicleInfo;

        //        public void LoadTopForm()
        //        {
        //            System.Text.StringBuilder sbJson = new StringBuilder();
        //            string strf = oGoodsSearch.dicQuery.ContainsKey("f") ? oGoodsSearch.dicQuery["f"] : "";
        //            sbJson.Append("{");
        //            sbJson.Append("\"f\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(strf) + "\"");
        //            sbJson.Append(",\"query\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(oGoodsSearch.GetQueryStr("", "")) + "\"");
        //            sbJson.Append(",\"nav_rows\":" + oGoodsSearch.GetSelectedLinkJson());
        //            sbJson.Append(",\"form_rows\":" + oGoodsSearch.GetFilterDataJson());
        //            sbJson.Append("}");

        //            //            this.ExecJScript(@"debugger;
        //            //");
        //            this.ExecJScript(@" var data= " + sbJson.ToString() + @" ; 
        //doBoundTopForm( data,'');");
        //        }

        public void LoadGoodsList(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            this.ExecJScript(@" var data= " + oGoodsSearch.GetGoodsList(CurrPageNum, strPageRowsCount, strSort) + @" ; 
                doBoundGoodsList( data,'');");
        }

        public override void DoAction()
        {
            if (this.strAction == "loadVehiclelistdata")
            {
                GetVehicleList();
                //GetVehicleListJavaScript();
                //this.ExecJScript("loadSetBrand('" + this.GetParamHtValue("HOT_LEVEL") + "','" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(LoadBrandListContent(this.GetParamHtValue("HOT_LEVEL"), 4).ToString()) + "');");
            }

            if (this.strAction == "GetChangedArea")
            {
                SetChangedArea();
            }

            if (this.strAction == "init")
            {
                RefreshArea();
                base.DoAction();
            }
        }

        private void SetChangedArea()
        {
            string controlWrap = this.GetParamHtValue("controlWrap");

            if (string.IsNullOrEmpty(controlWrap))
            {
                return;
            }

            string parentId = string.IsNullOrEmpty(this.GetParamHtValue("parentId")) ? "" : this.GetParamHtValue("parentId");

            string controlId = string.IsNullOrEmpty(this.GetParamHtValue("controlId")) ? "" : this.GetParamHtValue("controlId");

            bool IsHasAll = this.GetParamHtValue("IsHasAll") == "Y";

            DataSet ds;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds = objCBizCountryCity.SelectCountryByParentId(parentId);
            }

            //string execHtml = "$('#" + controlId + "').html('" + omQtHtmlGet.GetAreaSelectString(parentId, controlId, IsHasAll) + "')";

            //this.ExecJScript(execHtml);
        }


        private void GetVehicleList()
        {

            #region 获取前台参数
            string strf = string.IsNullOrEmpty(this.GetParamHtValue("f")) ? "" : "%" + HttpUtility.UrlDecode(this.GetParamHtValue("f")) + "%";

            string page = string.IsNullOrEmpty(this.GetParamHtValue("page")) ? "1" : this.GetParamHtValue("page");

            string country = string.IsNullOrEmpty(this.GetParamHtValue("country")) ? "DF6723BE-A25C-4F77-BB49-A1662D3E6F2A" : this.GetParamHtValue("country");
            string city = string.IsNullOrEmpty(this.GetParamHtValue("city")) ? "AEF39D8E-D298-4AF1-AEC5-2B00D237F5F5" : this.GetParamHtValue("city");
            string des = this.GetParamHtValue("des");

            int vtype = 0;
            if (!string.IsNullOrEmpty(this.GetParamHtValue("vtype")))
            {
                int.TryParse(this.GetParamHtValue("vtype"), out vtype);
            }
            int brand = 0;
            if (!string.IsNullOrEmpty(this.GetParamHtValue("brand")))
            {
                int.TryParse(this.GetParamHtValue("brand"), out brand);
            }

            int stype = 0;
            if (!string.IsNullOrEmpty(this.GetParamHtValue("stype")))
            {
                int.TryParse(this.GetParamHtValue("stype"), out stype);
            }

            string vprice = string.IsNullOrEmpty(this.GetParamHtValue("vprice")) ? "0|0" : this.GetParamHtValue("vprice");

            string seats = string.IsNullOrEmpty(this.GetParamHtValue("seats")) ? "0|0" : this.GetParamHtValue("seats");

            decimal maxPirce = string.IsNullOrEmpty(vprice) ? 0 : Convert.ToDecimal(vprice.Split('|')[1]);
            decimal minPirce = string.IsNullOrEmpty(vprice) ? 0 : Convert.ToDecimal(vprice.Split('|')[0]);

            int maxSeats = string.IsNullOrEmpty(seats) ? 0 : Convert.ToInt32(seats.Split('|')[1]);
            int minSeats = string.IsNullOrEmpty(seats) ? 0 : Convert.ToInt32(seats.Split('|')[0]);
            #endregion

            //设置翻页时的参数
            string queruStr = "country=" + HttpUtility.UrlEncode(country) + "&city=" + HttpUtility.UrlEncode(city) + "&des=" + HttpUtility.UrlEncode(des) + "&vtype=" + vtype + "&brand=" + brand + "&vprice=" + HttpUtility.UrlEncode(vprice) + "&seats=" + HttpUtility.UrlEncode(seats) + "&stype=" + stype + "&f=" + HttpUtility.UrlEncode(strf);

            this.ExecJScript(@"doBoundGoodsList(" + objVehicleInfo.GetVehicleList(page, "", "", des, city, country, vtype, brand, minPirce, maxPirce, minSeats, maxSeats, stype, strf) + ");$('#txtQuery').val('" + queruStr + "');$('#cityWrap').html('" + omQtHtmlGet.GetAreaSelectString(country, "citySel", false) + "');$('#desWrap').html('" + omQtHtmlGet.GetAreaSelectString(city, "destinationSel", true) + "')");
        }

        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#citySel').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#destinationSel').html(render);
                            });");
            }
        }
    }
}